package com.example.musicplayer.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.musicplayer.R
import com.example.musicplayer.model.SongList

class SongListAdapter(
    private val songLists: List<SongList>,
    private val onItemClickListener: OnItemClickListener,
    private val backgroundColor: Int
) : RecyclerView.Adapter<SongListAdapter.ViewHolder>() {

    interface OnItemClickListener {
        fun onSongListClick(position: Int)
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val title: TextView = itemView.findViewById(R.id.title)
        val count: TextView = itemView.findViewById(R.id.count)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val itemView = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_song_list, parent, false)
        return ViewHolder(itemView)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val songList = songLists[position]
        holder.title.text = songList.name
        holder.count.text = "${songList.songCount} 首"

        // 设置统一的背景色
        holder.itemView.setBackgroundColor(backgroundColor)

        // 设置点击监听器
        holder.itemView.setOnClickListener {
            onItemClickListener.onSongListClick(position)
        }
    }

    override fun getItemCount(): Int {
        return songLists.size
    }
}